﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.International.Converters.PinYinConverter;

namespace MillerApp.CommonLib.Utility
{
    /// <summary>
    /// 字符串帮助类
    /// </summary>
    public class StringHelper
    {
        //轉半角的函數(DBC case)
        //半角字符(33-126)与全角(65281-65374)的對應關系是:均相差
        public static string StrToDBC(string str)
        {
            char[] c = str.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32; continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }

        //轉全角的函數(SBC case)
        //全角(65281-65374)与半角字符(33-126)的對應關系是:均相差
        public static string StrToSBC(string str)
        {
            char[] c = str.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288; continue;
                }
                if (c[i] < 127) c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }

        /// <summary>
        /// 分成動態數據,自定義分割符
        /// </summary>
        /// <param name="s"></param>
        /// <param name="lsp"></param>
        /// <param name="rsp"></param>
        /// <returns></returns>
        public static List<string> StringToList(ref string  s, string lsp, string rsp)
        {
            string re = lsp + ".*?" + rsp;
            MatchCollection mc = Regex.Matches(s, re);
            List<string> list = new List<string>();
            for (int i = 0; i < mc.Count; i++)
            {
                list.Add(mc[i].Value.Substring(1, mc[i].Value.Length - 2));
                s = s.Replace(mc[i].Value, "");
            }
            return list;
        }

        /// <summary>
        /// 获取汉字的拼音
        /// </summary>
        /// <param name="str">汉字字符串</param>
        /// <returns>汉字拼音集[如果不是汉字，则直接返回原字符]</returns>
        public static List<string> GetPinyins(string str)
        {
            if (str == null)
            {
                str = string.Empty;
            }
            List<string> pinyins = new List<string>();
            foreach (char item in str.Trim())
            {
                if (ChineseChar.IsValidChar(item)==false)
                {
                    pinyins.Add(item.ToString());
                }
                else
                {
                    ChineseChar ch = new ChineseChar(item);
                    pinyins.Add(ch.Pinyins[0].Substring(0, 1).ToUpper() + ch.Pinyins[0].Substring(1, ch.Pinyins[0].Length - 2));
                }                
            }
            return pinyins;
        }
        /// <summary>
        /// 获取字符串拼音首字母字符串
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="acronyms">拼音字符串集</param>
        /// <returns>字符串拼音首字母字符串(全大写)</returns>
        public static string GetAcronyms(string str, out List<string> pinyins)
        {
            string result = string.Empty;
            pinyins = GetPinyins(str);
            foreach (var item in pinyins)
            {
                result += item.Substring(0, 1).ToUpper();
            }
            return result;
        }

        /// <summary>
        /// 把List转换成SQL语句:'???','???'
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="list">list</param>
        /// <returns>'****','****','****'</returns>
        public static string ConvertListToSQLString<T>(List<T> list)
        {
            if (list == null || list.Count == 0)
            {
                return "''";
            }
            StringBuilder strs = new StringBuilder();
            foreach (T itme in list)
            {
                strs.Append(itme.ToString().Replace("'", "''") + "','");
            }
            if (strs.Length > 0)
            {
                strs.Remove(strs.Length - 2, 2);
            }
            return "'" + strs.ToString();
        }
    }
}
